<#
.SYNOPSIS
    Convert one pptx file to pdf.
.DESCRIPTION
    Convert one pptx file to pdf.
.PARAMETER pptxfile
    pptx filename.
#>
param(
    [Parameter(Mandatory = $true)]
    [ValidateScript( {
        if (!(Test-Path $_ -PathType Leaf)) {
            throw "$_ is not existing!"
        } else {
            $true
        }
    })]
    [String] $pptxfile
)

# echo "$($pptxfile)"
# Write-Host "$($pptxfile)"
$pptxfile = Resolve-Path $pptxfile
# Write-Host "$($pptxfile)"
# $pptxfile = Resolve-Path $pptxfile | Get-Item -LiteralPath $_
$pptxf = Get-Item -LiteralPath $pptxfile
# Write-Host "$($pptxf.BaseName)"

# Swap out PPTX with PDF in the Filename
# $Name = ($pptxfile.Fullname).replace("pptx","pdf")
$Name = "$($pptxf.DirectoryName)\$($pptxf.BaseName).pdf"
if (Test-Path $Name -PathType Leaf) {
    # Write-Error -Message "pdf file is existing!" -ErrorAction Stop
    throw "pdf file is existing!"
} else {
    # Write-Host "$($pptxf.fullname)"
    $PowerPoint = NEW-OBJECT -COMOBJECT POWERPOINT.APPLICATION
    # Write-Host $PowerPoint
    # PowerPoint.Visible = $True
    # open a PowerPoint document, filename from the directory
    $Doc = $PowerPoint.Presentations.Open($pptxf.fullname)
    # Write-Host $Doc
    
    # $document = $word_app.Presentations.Open($_.FullName)
    # $pdf_filename = "$($_.DirectoryName)\$($_.BaseName).pdf"

    $Doc.saveas([ref] $Name, [ref] 32)

    $Doc.close()
    $PowerPoint.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Doc) | Out-Null
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($PowerPoint) | Out-Null
    Remove-Variable Doc,PowerPoint
}

Remove-Variable pptxf,Name
[gc]::collect()
[gc]::WaitForPendingFinalizers()
